﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GUI
{
    public partial class SubFormThemCTHoaDon : Window
    {
        public SubFormThemCTHoaDon(int MaHD)
        {
            InitializeComponent();
            lbMaHD.Text = MaHD.ToString();
            hoiThoat = false;
        }

        private void cbbTenSP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (txtSL.Text == "")
                txtSL.Text = "0";
            DTO.SanPhamDTO sp = (SanPhamDTO)cbbTenSP.SelectedItem;
            BUS.HoaDonBUS hd = new HoaDonBUS();
            txtDonGia.Text = sp.DonGiaKL.ToString();
            if (hd.IsMember(int.Parse(lbMaHD.Text)) == 1)
                txtDonGia.Text = sp.DonGiaTV.ToString();
        }

        private void txtSL_TextChanged(object sender, EventArgs e)
        {
            if (txtSL.Text == "" || txtDonGia.Text == "")
                return;
            int sl = int.Parse(txtSL.Text);
            int dg = int.Parse(txtDonGia.Text);
            int cv=int.Parse(txtDiemCV.Text);
            txtThanhTien.Text = (sl * dg).ToString();
            txtCVTong.Text = (sl*cv ).ToString();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {

            CTHoaDonBUS cthdbus = new CTHoaDonBUS();
            CTHoaDonDTO cthd = new CTHoaDonDTO();
            cthd.MaHD = int.Parse(lbMaHD.Text);
            SanPhamDTO sp = (SanPhamDTO)cbbTenSP.SelectedItem;
            cthd.MaSP = sp.MaSP;
            cthd.SoHD = "0";
            cthd.TenSP = sp.TenSP;
            cthd.SoLuong = int.Parse(txtSL.Text);
            cthd.DonGia = int.Parse(txtDonGia.Text);
            cthd.ThanhTien = double.Parse(txtThanhTien.Text);
            cthd.DiemCV = int.Parse(txtCVTong.Text);
            if (cthd.SoLuong <= 0)
            {
                MessageBox.Show("So Luong Phai >0");
                txtSL.Focus();
            }
            else
            {
                try
                {
                    cthdbus.themCTvaoHDDaTonTai(cthd);
                }
                catch (Exception)
                {
                    MessageBox.Show("San Pham Da Co Trong Danh Sach");
                    cbbTenSP.Focus();
                }
                this.Close();
            }
            
        }

        private void SubFormThemCTHoaDon_Load(object sender, EventArgs e)
        {
            BUS.SanPhamBUS sp = new BUS.SanPhamBUS();
            cbbTenSP.DataSource = sp.SanPhamNotInCTHoaDon(int.Parse(this.lbMaHD.Text));
            cbbTenSP.DisplayMember = "TenSP";
            DTO.SanPhamDTO spDuocChon = (SanPhamDTO)cbbTenSP.SelectedItem;
            BUS.HoaDonBUS hd = new HoaDonBUS();
            txtDonGia.Text = spDuocChon.DonGiaKL.ToString();
            if (hd.IsMember(int.Parse(lbMaHD.Text)) == 1)
                txtDonGia.Text = spDuocChon.DonGiaTV.ToString();
            txtDiemCV.Text = spDuocChon.DiemCV.ToString();
            txtThanhTien.Text = "0";
            txtSL.Text = "0";
            txtCVTong.Text = "0";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
